<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= entityAbsolutePackage %>.service.mapper;

<%_
  const getOriginalField = field => field.originalField || field;
  const uuidMapMethod = restProperties.some(property => property.relatedField?.fieldTypeUUID);
  const byteMapMethod = restProperties.some(property => property.relatedField?.fieldTypeBytes);
  const dtoRelationships = restProperties.filter(property => property.relationshipName && !property.otherEntity.embedded);
  let otherEntitiesFields = otherEntities
    .filter(otherEntity => dtoRelationships.some(relationship => relationship.otherEntity === otherEntity))
    .map(otherEntity =>
      dtoRelationships
        .filter(relationship => relationship.otherEntity == otherEntity)
        .map(({relatedField, collection}) => ({otherEntity, relatedField, collection}))
    )
    .flat();
  otherEntitiesFields.forEach(a => {
    a.collection = a.collection || otherEntitiesFields.some(b => a.otherEntity === b.otherEntity && getOriginalField(a.relatedField) === getOriginalField(b.relatedField) && b.collection === true)
  });
  otherEntitiesFields = this._.uniqWith(otherEntitiesFields, (a, b) => a.otherEntity === b.otherEntity && a.collection === b.collection && getOriginalField(a.relatedField) === getOriginalField(b.relatedField));
_%>

<%_ if (dtoRelationships.some(r => r.collection)) { _%>
import java.util.Set;
import java.util.stream.Collectors;
<%_ } _%>

import <%= entityAbsoluteClass %>;
import <%= entityAbsolutePackage %>.service.dto.<%= dtoClass %>;

<%_ for (const otherEntity of this._.uniq(dtoRelationships.map(relationship => relationship.otherEntity).filter(otherEntity => otherEntity.entityClass !== entityClass))) { _%>
import <%= otherEntity.entityAbsoluteClass %>;
import <%= otherEntity.entityAbsolutePackage %>.service.dto.<%= otherEntity.dtoClass %>;
<%_ } _%>

import org.mapstruct.*;
<%_ if (uuidMapMethod) { _%>

import java.util.Objects;
import java.util.UUID;
<%_ } _%>

/**
 * Mapper for the entity {@link <%= persistClass %>} and its DTO {@link <%= dtoClass %>}.
 */
@Mapper(componentModel = "spring")
public interface <%= entityClass %>Mapper extends EntityMapper<<%= dtoClass %>, <%= persistClass %>> {
<%_ if (!embedded) { _%>
  <%_ var renMapAnotEnt = false; /*Render Mapping Annotation during Entity to DTO conversion?*/ _%>
  <%_ for (relationship of dtoRelationships) { _%>
    <%_
      renMapAnotEnt = true;
      let qualifiedByName = relationship.otherEntity.entityInstance + this._.upperFirst(relationship.otherEntityField);
      qualifiedByName = qualifiedByName + (relationship.collection ? 'Set' : '');
    _%>
    @Mapping(target = "<%= relationship.propertyName %>", source = "<%= relationship.propertyName %>", qualifiedByName="<%= qualifiedByName %>")
  <%_ } _%>
  <%_ for (const field of fields.filter(field => field.mapstructExpression)) {
      renMapAnotEnt = true; _%>
    @Mapping( target = "<%= field.propertyName %>", expression = "<%- field.mapstructExpression %>")
  <%_ } _%>
  <%_ if (renMapAnotEnt) { _%>
    <%= dtoClass %> toDto(<%= persistClass %> s);
  <%_ } _%>

  <%_ var renMapAnotDto = false;  /*Render Mapping Annotation during DTO to Entity conversion?*/ _%>
  <%_ if(primaryKey.ids.length > 1) { _%>
    <%_ renMapAnotDto = true; _%>
    <%_ for (const id of primaryKey.ids) { _%>
    @Mapping(target = "id.<%= id.name %>", source = "<%= id.nameDotted %>")
    <%_ } _%>
  <%_ } _%>
  <%_ for (relationship of dtoRelationships) { _%>
    <%_ if (!relationship.ownerSide) { _%>
      <%_ renMapAnotDto = true; _%>
    @Mapping(target = "<%= relationship.propertyName %>", ignore = true)
    <%_ }
    if (relationship.collection && fluentMethods) {
      renMapAnotDto = true; _%>
    @Mapping(target = "remove<%= relationship.relationshipNameCapitalized %>", ignore = true)
    <%_ } _%>
  <%_ } _%>
  <%_ if (renMapAnotDto) { _%>
    <%= persistClass %> toEntity(<%= dtoClass %> <%= dtoInstance %>);
  <%_ } _%>
  <%_ for (const {otherEntity, relatedField, collection} of otherEntitiesFields) { _%>
    <%_ const mapperName = otherEntity.entityInstance + this._.upperFirst(relatedField.propertyName); _%>

    @Named("<%= mapperName %>")
    @BeanMapping(ignoreByDefault = true)
    <%_ for (const field of otherEntity.primaryKey.fields) { _%>
    @Mapping(target = "<%= field.propertyName %>", source = "<%= field.propertyName %>")
    <%_ } _%>
    <%_ if (!relatedField.id) { _%>
      <%_ if (relatedField.mapstructExpression) { _%>
    @Mapping(target = "<%= relatedField.propertyName %>", expression = "<%- relatedField.mapstructExpression %>")
      <%_ } else { _%>
    @Mapping(target = "<%= relatedField.propertyName %>", source = "<%= relatedField.propertyName %>")
      <%_ } _%>
    <%_ } _%>
      <%_ if (relatedField.mapstructExpression) { _%>
    <%- otherEntity.dtoClass %> toDto<%= this._.upperFirst(mapperName) %>(<%- otherEntity.persistClass %> s);
    <%_ } else { _%>
    <%- otherEntity.dtoClass %> toDto<%= this._.upperFirst(mapperName) %>(<%- otherEntity.persistClass %> <%= otherEntity.persistInstance %>);
      <%_ } _%>
    <%_ if (collection) { %>
      <%_ const collectionMapperName = otherEntity.entityInstance + this._.upperFirst(relatedField.propertyName) + 'Set'; _%>

    @Named("<%= collectionMapperName %>")
    default Set<<%- otherEntity.dtoClass %>> toDto<%= this._.upperFirst(mapperName) %>Set(Set<<%- otherEntity.persistClass %>> <%= otherEntity.persistInstance %>) {
        return <%= otherEntity.persistInstance %>.stream().map(this:: toDto<%= this._.upperFirst(mapperName) %>).collect(Collectors.toSet());
    }
    <%_ } _%>
  <%_ } _%>
  <%_ if (uuidMapMethod) { _%>

    default String map(UUID value) {
        return Objects.toString(value, null);
    }
  <%_ } _%>
  <%_ if (byteMapMethod) { _%>

    default String map(byte[] value) {
        return new String(value);
    }
  <%_ } _%>
<%_ } _%>
}
